Skip to content

Publicize TreeEntryDefinition.From() overloads#547

Merged
nulltoken merged 2 commits intolibgit2:vNextfrom
dahlbyk:TreeEntryDef-From
Nov 11, 2013
Merged

Publicize TreeEntryDefinition.From() overloads#547
nulltoken merged 2 commits intolibgit2:vNextfrom
dahlbyk:TreeEntryDef-From

Conversation

@dahlbyk
Copy link
Member

@dahlbyk dahlbyk commented Nov 4, 2013

Branched from #511. As written there:

My desired behavior was essentially "inject cleansed file from a branch" into earlier trees. Without TreeEntryDefinition.From(te), I have to do something like:

var blob = (Blob)te.Target;
td.Add(te.Path, blob, blob.Mode);

Not a huge deal, but I resent the extra work. Things would get even more complicated if you're dealing with more than one type of TreeEntry.

The first commit has the two overloads I think we definitely need:

  • From(TreeEntry treeEntry) for the case above, where we just want to use an existing TreeEntry.
  • From(ObjectId objectId) since we can't naturally get access to a Submodule instance during a rewrite.

The second commit includes the other two overloads, for completeness. I can't think of a reason we would need to publicize either, since they align with existing TreeDefinition.Add(...) overloads, but I'm not sure it hurts to expose them if the others are too.

@nulltoken
Copy link
Member

The first commit has the two overloads I think we definitely need:

  • From(TreeEntry treeEntry) for the case above, where we just want to use an existing TreeEntry.

How about rather adding a new overload TreeDefinition.Add(string, TreeEntry)? Wouldn't that serve a similar purpose?

  • From(ObjectId objectId) since we can't naturally get access to a Submodule instance during a rewrite.

Can't we use the existing TreeDefinition.AddGitLink(string, ObjectId) here?

@dahlbyk
Copy link
Member Author

dahlbyk commented Nov 4, 2013

How about rather adding a new overload TreeDefinition.Add(string, TreeEntry)?

👍

Can't we use the existing TreeDefinition.AddGitLink(string, ObjectId) here?

Not sure how I overlooked that... yes.

You could have just told me I was an idiot in the other PR. 😊

@nulltoken
Copy link
Member

You could have just told me I was an idiot in the other PR. 😊

There was something bothering me about the initial proposal but I couldn't point at it earlier.

And, no. You're not an idiot.

You're my hero

@dahlbyk
Copy link
Member Author

dahlbyk commented Nov 5, 2013

🙉 🙈 🙊

Much smaller change for your consideration.

@nulltoken
Copy link
Member

@dahlbyk Nice!

I was wondering if could take a fresh look at TreeDefinition.From(Tree). This calls an AddEntry method (that your code doesn't rely on).

  • Either AddEntry is useful at this level, and maybe this new Add() overload should rely on it
  • Or TreeDefinition.From(Tree) should directly call Add(string, TreeEntry) (As Add(string, TreeEntryDefinition) eventually seem to invoke AddEntry).

Last request: Could you please cover with some test this new overload. Imaginary bonus points if it demonstrates how simpler this makes the history rewriting process.

@dahlbyk
Copy link
Member Author

dahlbyk commented Nov 5, 2013

Could you please cover with some test this new overload.

Beyond the tests already included? I can add a filter-branch test as an example, too...

@dahlbyk
Copy link
Member Author

dahlbyk commented Nov 5, 2013

Either AddEntry is useful at this level, and maybe this new Add() overload should rely on it

This seems like the direction to go, with Add(string, TreeEntry) as a thin wrapper that provides validation. Though I'm not sure exactly what it is that the wrapped/unwrapped mechanism is doing, honestly.

@nulltoken
Copy link
Member

Though I'm not sure exactly what it is that the wrapped/unwrapped mechanism is doing, honestly.

@dahlbyk A "wrapped" Tree is an already existing odb Tree. Once, you start altering it, by adding, removing or modifying any of its content, it "unwraps" (and also unwraps its parent Trees if any) and it's tracked as an entry in the unwrappedTrees dictionary.

Adding a new TreeEntryDefinition automatically replaces the potentially existing one. As such, AddEntry() takes care of removing any unwrapped Tree matching the key/name.

Considering this, I think it's safe to remove the call to AddEntry() from TreeDefintion.From(Tree). Could you please embed this as part of this PR?

diff --git a/LibGit2Sharp/TreeDefinition.cs b/LibGit2Sharp/TreeDefinition.cs
index 7426a28..63bbb0c 100644
--- a/LibGit2Sharp/TreeDefinition.cs
+++ b/LibGit2Sharp/TreeDefinition.cs
@@ -29,7 +29,7 @@ public static TreeDefinition From(Tree tree)

             foreach (TreeEntry treeEntry in tree)
             {
-                td.AddEntry(treeEntry.Name, TreeEntryDefinition.From(treeEntry));
+                td.Add(treeEntry.Name, treeEntry);
             }

             return td;

@dahlbyk
Copy link
Member Author

dahlbyk commented Nov 11, 2013

Considering this, I think it's safe to remove the call to AddEntry() from TreeDefintion.From(Tree). Could you please embed this as part of this PR?

Done

@dahlbyk
Copy link
Member Author

dahlbyk commented Nov 11, 2013

Rebased on latest. Also added a test that shows rewriting history to always use a specific version of a TreeEntry.

@nulltoken
Copy link
Member

I love it!

Let's squash this! 🔨

@dahlbyk
Copy link
Member Author

dahlbyk commented Nov 11, 2013

Squashed first two...figure the last can stand alone.

@nulltoken nulltoken merged commit c890e61 into libgit2:vNext Nov 11, 2013
@nulltoken
Copy link
Member

Thanks 🍌 ‼️

@dahlbyk dahlbyk deleted the TreeEntryDef-From branch November 11, 2013 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants